<?php

namespace yan\backendclient\models;

use Yii;

/**
 * This is the model class for table "{{%backend_menu}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $url
 * @property integer $pid
 * @property integer $status
 * @property string $i18n
 * @property string $class
 * @property integer $created_at
 * @property integer $updated_at
 */
class Menu extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%backend_menu}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'i18n'], 'required'],
            [['pid', 'status', 'sort_order', 'created_at', 'updated_at'], 'integer'],
            [['name', 'url',  'i18n', 'class'], 'string', 'max' => 255],
            [['module_id', 'controller_id',  'action_id', 'type'], 'string', 'max' => 255],
            // [['name'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('debug', 'ID'),
            'name' => Yii::t('debug', 'Name'),
            'url' => Yii::t('debug', 'Url'),
            'pid' => Yii::t('debug', 'Pid'),
            'status' => Yii::t('debug', 'Status'),
            'i18n' => Yii::t('debug', 'I18n'),
            'class' => Yii::t('debug', 'Icon class'),
            'sort_order'=> Yii::t('debug', 'Sort Order'),
            'created_at' => Yii::t('debug', 'Created At'),
            'updated_at' => Yii::t('debug', 'Updated At'),
            'module_id' => Yii::t('debug', 'Module ID'),
            'controller_id' => Yii::t('debug', 'Controller ID'),
            'action_id' => Yii::t('debug', 'Action ID'),
            'type' => Yii::t('debug', 'Parameter'),
        ];
    }

    public function behaviors()
    {
        return \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ]);
    }
    // 保存操作
    public function doSave(){
        if( empty($this->url) && ( empty($this->module_id) && empty($this->controller_id) ) ){
            $this->addError('module_id', Yii::t('debug', 'Url or module cannot all blank.'));
            return false;
        }
        if(empty($url)){
            $this->controller_id = empty($this->controller_id) ? 'default' : $this->controller_id;
            $this->action_id = empty($this->action_id) ? 'index' : $this->action_id;
        }
        return $this->save();
    }

    // 設置後臺連接
    public static function setBackendUrl($data){
        if( !empty($data['url']) && false ){
            if(preg_match("/^\//", $data['url'])){
                return [$url, 'type'=>!empty($data['type']) ? $data['type'] : null];
            }else{
                return $url;
            }
        }else{
            $r = !empty($data['module_id']) ? '/'. $data['module_id'] : '';
            $r .= !empty($data['controller_id']) ? '/'. $data['controller_id'] : 'default';
            $r .= !empty($data['action_id']) ? '/'. $data['action_id'] : 'index';
            return [$r, 'type'=>!empty($data['type']) ? $data['type'] : null];
        }
        
    }
}
